/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package iddss;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;


/**
 *
 * @author ZeM
 */
public class ClientWorker implements Runnable {

    static int connection_number;
    static Socket client;
    static DataInputStream dis;
    static DataOutputStream dos;
  

//Constructor
    ClientWorker(Socket clientz, int connection_numberz) {
        client = clientz;
        connection_number = connection_numberz;
        Parameters.connection_is_used[connection_number] = true;

        dis = null;
        dos = null;
    }

    private void CreateIO() {
        try {
            dos = new DataOutputStream(client.getOutputStream());
            dis = new DataInputStream(client.getInputStream());
        } catch (Exception error) {
            System.out.println(error + "CreateIO");
        }
    }

    private void CloseIO() {
        try {
            dis.close();
            dos.close();
            client.close();
            Parameters.connection_is_used[connection_number] = false;
        } catch (Exception error) {
            System.out.println("closeio" + error.fillInStackTrace());
        }
    }

    public void run() {


       

    }

    /**
     * Start main part
     * @param priority
     */
    private void main(byte priority) {
        try {
            //get info what to do
            boolean exit = false;
            do {
                byte state = getByte();

                if (state == 0) { //if exit
                    exit = true;
                } else { //if not exit


                    if (state == 1) {
                        //load plugin
                        //get plugin name
                        String plugin_name = getUTF();
                        //load plugin
                      



                    } else if (state == 2) {
                        //System command
                        byte command = getByte();

                        if (command == 1) {
                            //exit from client
                            System.exit(0);
                        }
                        if (command == 2) {
                            //reload client
                            }
                    }
                }

            } while (!exit);

        } catch (Exception ex) {
            System.out.println("ClientWorker.main:" + ex);
        }
    }

    public String getUTF() {
        try {
            String message = dis.readUTF();
            dos.writeBoolean(true);
            dos.flush();
            return message;
        } catch (IOException ex) {
            System.out.println("ClientWorker.getUTF():" + ex);
            return null;
        }
    }

    public byte[] getBytes() {
        try {
            int length = dis.readInt();
            byte[] message = new byte[length];
            dos.writeBoolean(true);
            dos.flush();
            dis.read(message, 0, length);
            dos.writeBoolean(true);
            dos.flush();
            return message;
        } catch (IOException ex) {
            System.out.println("ClientWorker.getBytes():" + ex);
            return null;
        }
    }

    public byte getByte() {
        try {
            byte message;
            message = dis.readByte();
            dos.writeBoolean(true);
            dos.flush();
            return message;
        } catch (IOException ex) {
            return -128;
        }
    }

    /**
     * Sending byte message to server
     * @param message byte that need to send
     * @return true if delivered successfully
     */
    public boolean SendMessageByte(byte message) {
        //sending message to client
        //return true if delivered
        try {
            dos.write(message);
            dos.flush();
            if (dis.readBoolean()) {
                return true;
            } else {
                return false;
            }
        } catch (Exception error) {
            return false;
        }

    }

    /**
     * Sending byte message to server
     * @param message array of bytes that need to send
     * @return true if delivered successfully
     */
    public boolean SendMessageBytes(byte[] message) {
        //sending message to client
        //return true if delivered
        try {
            dos.writeInt(message.length);
            dos.flush();
            if (dis.readBoolean()) {
                dos.write(message, 0, message.length);
                dos.flush();
                if (dis.readBoolean()) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } catch (Exception error) {
            return false;
        }

    }

    /**
     * Sending string message to server
     * @param message string that need to send
     * @return true if delivered successfully
     */
    public boolean SendMessageUTF(String message) {
        //sending message to client
        //return true if delivered
        try {
            dos.writeUTF(message);
            dos.flush();
            if (dis.readBoolean()) {
                return true;
            } else {
                return false;
            }
        } catch (Exception error) {
            return false;
        }

    }
}
